package org.rockyang.dike.conf;

import org.rockyang.dike.core.AuthorizationRealm;
import org.rockyang.dike.core.SubjectManager;
import org.rockyang.dike.core.impl.AuthorizationManager;
import org.rockyang.dike.interceptor.AuthorizationInterceptor;
import org.rockyang.dike.surpport.ShiroxConfigure;
import org.rockyang.dike.thymeleaf.dialect.DikeDialect;
import org.rockyang.dike.utils.AuthorizationUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author yangjian
 * @since 17-12-11.
 */
@Configuration
public class MyShiroxConf extends WebMvcConfigurationSupport {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new AuthorizationInterceptor()).addPathPatterns("/**").excludePathPatterns("/error");
		super.addInterceptors(registry);
	}

	@Bean
	public AuthorizationRealm shiroxRealm(){
		ShiroRealm shiroRealm = new ShiroRealm();
		return shiroRealm;
	}

	@Bean
	public SubjectManager subjectManger() {
		AuthorizationManager manager = new AuthorizationManager();
		manager.addRealm(shiroxRealm());
		ShiroxConfigure configure = new ShiroxConfigure();
		configure.setLoginUrl("/login");
		configure.addWhiteList("/doLogin");
		configure.setUnAuthorizedUrl("/403");
		manager.setShiroxConfigure(configure);
		AuthorizationUtils.bindAuthorizationManager(manager);
		return manager;
	}

	@Bean
	public DikeDialect dikeDialect() {
		return new DikeDialect();
	}
}
